/**
 * 
 */
package infocluster.ui;

import java.io.IOException;

import infocluster.engine.ExcelProcessEngine;
import infocluster.resource.ResourceLocator;
import infocluster.ui.taskpane.prototype.*;
import infocluster.ui.taskpane.CleanUpPane;
import infocluster.ui.taskpane.ComparePane;
import infocluster.ui.taskpane.OfferUpdatePane;
import infocluster.ui.taskpane.ReshapePane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;

/**
 * @author 08302010017 Xi Qi
 *
 */
public class Frame {

	private static final ExcelProcessEngine ENGINE = ExcelProcessEngine.getEngine();
	
	private static final int STAGE_RESHAPE = 0;
	private static final int STAGE_CLEANUP = 1;
	private static final int STAGE_UPDATE = 2;
	private static final int STAGE_COMPARE = 3;
	
	private static final String BACK_IMAGE = "/img/background.png";
	
	private static final String PRO_RESHAPE = "/img/process-reshape.png";
	private static final String PRO_CLEANUP = "/img/process-cleanup.png";
	private static final String PRO_UPDATE = "/img/process-update.png";
	private static final String PRO_COMPARE = "/img/process-compare.png";
	
	private static final String PRO_RESHAPE_ON = "/img/process-reshape-on.png";
	private static final String PRO_CLEANUP_ON = "/img/process-cleanup-on.png";
	private static final String PRO_UPDATE_ON = "/img/process-update-on.png";
	private static final String PRO_COMPARE_ON = "/img/process-compare-on.png";
	
	private static final String PRO_RESHAPE_ED = "/img/process-reshape-ed.png";
	private static final String PRO_CLEANUP_ED = "/img/process-cleanup-ed.png";
	private static final String PRO_UPDATE_ED = "/img/process-update-ed.png";
	private static final String PRO_COMPARE_ED = "/img/process-compare-ed.png";
	
	private static final String[] MONTH_CN = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
	private static final String[] MONTH_EN = {"Jan","Feb","Mar","Apr","May","June","Jul","Aug","Sept","Oct","Nov","Dec"};

	private static int stage = -1;
	
	public Frame(){
		Display display = new Display();
		final Shell shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.BORDER
				| SWT.APPLICATION_MODAL);
		int width = shell.getMonitor().getClientArea().width;
		int height = shell.getMonitor().getClientArea().height;
		int x = shell.getSize().x;
		int y = shell.getSize().y;
		if (x > width) {
		    shell.getSize().x = width;
		}
		if (y > height) {
		    shell.getSize().y = height;
		}
		shell.setText( "SCBL-InfoCluster" );
		shell.setBounds((width - x) / 2,(height - y) / 2,540,450);
		shell.setLayout(null);
		
		final Button buttonReshape = buildButton(shell, 10, 73, 121, 31, null,PRO_RESHAPE);
		final Button buttonOfferUpdate = buildButton(shell, 140, 73, 121, 31, null,PRO_UPDATE);
		final Button buttonCompare = buildButton(shell, 270, 73, 121, 31, null,PRO_COMPARE);
		final Button buttonCleanup = buildButton(shell, 400, 73, 121, 31, null,PRO_CLEANUP);
		final Composite taskPane = buildComposite(shell, 10, 120, 465, 750);
		
		
		final Combo combo = new Combo(shell, SWT.READ_ONLY);
		combo.setBounds(180,30,100,30);
		combo.setItems(MONTH_CN);
		combo.select(0);
		for(int i=0;i<12;i++){
			combo.setData(MONTH_CN[i],MONTH_EN[i]);
		}
		
		combo.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent arg0e) {
				String month = (String)combo.getData(combo.getText());
				ENGINE.setMonth(month);
				if(taskPane.getChildren().length != 0){
					TaskPane task = (TaskPane)taskPane.getChildren()[0];
					task.setMonth(month);
				}
			}
			
		});
		
		taskPane.setVisible(false);
		
		buttonReshape.addListener(SWT.MouseEnter, new Listener() {
			public void handleEvent(Event event) {
				if(stage != STAGE_RESHAPE)
					setButtonImage(buttonReshape, PRO_RESHAPE_ON);
			}
		});
		buttonReshape.addListener(SWT.MouseExit, new Listener() {
			public void handleEvent(Event event) {
				if(stage != STAGE_RESHAPE)
					setButtonImage(buttonReshape, PRO_RESHAPE);
			}
		});	
		buttonReshape.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				stage = STAGE_RESHAPE;
				setButtonImage(buttonReshape, PRO_RESHAPE_ED);
				setButtonImage(buttonOfferUpdate, PRO_UPDATE);
				setButtonImage(buttonCompare, PRO_COMPARE);
				setButtonImage(buttonCleanup, PRO_CLEANUP);
				ENGINE.resetTask();
				clearComposite(taskPane);
				new ReshapePane(shell, taskPane, SWT.NONE, ENGINE);
				taskPane.setVisible(true);
			}
		});			
		
		buttonOfferUpdate.addListener(SWT.MouseEnter, new Listener() {
			public void handleEvent(Event event) {
				if(stage != STAGE_UPDATE)
					setButtonImage(buttonOfferUpdate, PRO_UPDATE_ON);
			}
		});
		buttonOfferUpdate.addListener(SWT.MouseExit, new Listener() {
			public void handleEvent(Event event) {
				if(stage != STAGE_UPDATE)
					setButtonImage(buttonOfferUpdate, PRO_UPDATE);
			}
		});	
		buttonOfferUpdate.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				stage = STAGE_UPDATE;
				setButtonImage(buttonReshape, PRO_RESHAPE);
				setButtonImage(buttonOfferUpdate, PRO_UPDATE_ED);
				setButtonImage(buttonCompare, PRO_COMPARE);
				setButtonImage(buttonCleanup, PRO_CLEANUP);
				ENGINE.resetTask();
				clearComposite(taskPane);
				new OfferUpdatePane(shell, taskPane, SWT.NONE, ENGINE);
				taskPane.setVisible(true);
			}
		});
		
		buttonCompare.addListener(SWT.MouseEnter, new Listener() {
			public void handleEvent(Event event) {
				if(stage != STAGE_COMPARE)
					setButtonImage(buttonCompare, PRO_COMPARE_ON);
			}
		});
		buttonCompare.addListener(SWT.MouseExit, new Listener() {
			public void handleEvent(Event event) {
				if(stage != STAGE_COMPARE)
					setButtonImage(buttonCompare, PRO_COMPARE);
			}
		});	
		buttonCompare.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				stage = STAGE_COMPARE;
				setButtonImage(buttonReshape, PRO_RESHAPE);
				setButtonImage(buttonOfferUpdate, PRO_UPDATE);
				setButtonImage(buttonCompare, PRO_COMPARE_ED);
				setButtonImage(buttonCleanup, PRO_CLEANUP);
				ENGINE.resetTask();
				clearComposite(taskPane);
				new ComparePane(shell, taskPane, SWT.NONE, ENGINE);
				taskPane.setVisible(true);
			}
		});
		
		buttonCleanup.addListener(SWT.MouseEnter, new Listener() {
			public void handleEvent(Event event) {
				if(stage != STAGE_CLEANUP)
					setButtonImage(buttonCleanup, PRO_CLEANUP_ON);
			}
		});
		buttonCleanup.addListener(SWT.MouseExit, new Listener() {
			public void handleEvent(Event event) {
				if(stage != STAGE_CLEANUP)
					setButtonImage(buttonCleanup, PRO_CLEANUP);
			}
		});	
		buttonCleanup.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				stage = STAGE_CLEANUP;
				setButtonImage(buttonReshape, PRO_RESHAPE);
				setButtonImage(buttonOfferUpdate, PRO_UPDATE);
				setButtonImage(buttonCompare, PRO_COMPARE);
				setButtonImage(buttonCleanup, PRO_CLEANUP_ED);
				ENGINE.resetTask();
				clearComposite(taskPane);
				new CleanUpPane(shell, taskPane, SWT.NONE, ENGINE);
				taskPane.setVisible(true);
			}
		});	
		
		try {
			Image img = ResourceLocator.get().getImage(shell, BACK_IMAGE);
			shell.setBackgroundImage(img);
		} catch (IOException e) {
			e.printStackTrace();
		}
				

		shell.open();
		while ( ! shell.isDisposed()) {
			if ( ! display.readAndDispatch()) display.sleep();
		}
		display.dispose();
	}

	/**
	 * @param shell
	 */
	private Button buildButton(final Shell shell, int x, int y, int width, int height, String title, String image) {
		Button button = new Button(shell, SWT.PUSH|SWT.NO_BACKGROUND);
		button.setBounds( x, y , width , height );
		button.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		button.setVisible(true);
		if(title != null)
			button.setText(title);
		if(image != null){
			try {
				Image img = ResourceLocator.get().getImage(shell, image);
				button.setImage(img);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return button;
	}
	
	private void setButtonImage(Button b, String image){
		if(image != null){
			try {
				Image img = new Image(b.getDisplay(),this.getClass().getResource(image).openStream());
				b.setImage(img);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private Composite buildComposite(final Shell shell, int x, int y, int width, int height){
		Composite comp = new Composite(shell, SWT.NONE);
		comp.setBounds(x, y, width, height);
		comp.setVisible(true);
		comp.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		return comp;
	}
	
	private void clearComposite(Composite comp){
		Control []childs = comp.getChildren();
	    for(int i=0;i<childs.length;i++){
	    	Control temp=childs[i];
	    	if(!temp.isDisposed()){
	    		temp.dispose();
	    	}
	    }
	}

}
